package com.manong.bookstore.produce.service.impl;

import com.manong.common.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.Query;

import com.manong.bookstore.produce.dao.ProduceDao;
import com.manong.bookstore.produce.entity.ProduceEntity;
import com.manong.bookstore.produce.service.ProduceService;
import org.springframework.util.StringUtils;


@Service("produceService")
public class ProduceServiceImpl extends ServiceImpl<ProduceDao, ProduceEntity> implements ProduceService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<ProduceEntity> page = this.page(
//                new Query<ProduceEntity>().getPage(params),
//                new QueryWrapper<ProduceEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key
        String key = (String) params.get("key");
        QueryWrapper<ProduceEntity> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("produce_id",key)
                    .or().eq("store_id",key)
                    .or().eq("classify_id",key)
                    .or().eq("category_id",key)
                    .or().eq("produce_price",key)
                    .or().like("produce_name",key)
                    .or().like("produce_desc",key);
        }

        IPage<ProduceEntity> page = this.page(
                new Query<ProduceEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, long storeId) {
        String key = (String) params.get("key");
        QueryWrapper<ProduceEntity> wrapper = new QueryWrapper<ProduceEntity>().eq("store_id", storeId);
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.like("produce_name", key).or().like("produce_desc", key);
            });
        }
        IPage<ProduceEntity> page = this.page(new Query<ProduceEntity>().getPage(params), wrapper);
//        List<ProduceEntity> list = page.getRecords();
//        String limit= (String) params.get("limit");
//        Page pp = (Page) params.get("page");
//        System.out.println("_________+++++++++++++++++++++++++"+limit);
//        if(limit!=null && pp!=null){
//            int pageSize = Integer.parseInt(params.get("limit").toString());
//            int currentPage = (int) pp.getCurrent();
//
//            int total = list.size();
//            System.out.println(limit+" "+total+" "+pageSize+" "+currentPage);
//            page.setRecords(list.subList((currentPage - 1) * pageSize,
//                    (currentPage * pageSize) < list.size() ? (currentPage * pageSize) : list.size()));
//            page.setTotal(total);
//        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageClassify(Map<String, Object> params, long classifyId) {
        QueryWrapper<ProduceEntity> wrapper = new QueryWrapper<ProduceEntity>().eq("classify_id", classifyId);
        IPage<ProduceEntity> page = this.page(new Query<ProduceEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCategory(Map<String, Object> params, long categoryId) {
        QueryWrapper<ProduceEntity> wrapper = new QueryWrapper<ProduceEntity>().eq("category_id", categoryId);
        IPage<ProduceEntity> page = this.page(new Query<ProduceEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

}